﻿using System;
using System.Collections.Generic;
using SharpSqlAnalyzer.nodes;
using SharpSqlAnalyzer.nodes.hive;
using SharpSqlAnalyzer.pp.processor.type.comm;
using SharpSqlAnalyzer.pp.utils;
using SharpSqlAnalyzer.stmt;

namespace SharpSqlAnalyzer.pp.stmtformatter.type.comm
{
	// Token: 0x0200020B RID: 523
	public class InsertStmtFormatter : AbstractStmtFormatter
	{
		// Token: 0x0600121B RID: 4635 RVA: 0x00317DA3 File Offset: 0x00315FA3
		public virtual void addItemListProcessor(AbstractProcessor p)
		{
			if (this.itemListProcessors == null)
			{
				this.itemListProcessors = new List<AbstractProcessor>();
			}
			this.itemListProcessors.Add(p);
		}

		// Token: 0x0600121C RID: 4636 RVA: 0x00317DC4 File Offset: 0x00315FC4
		public virtual void addValueListProcessor(AbstractProcessor p)
		{
			if (this.valueListProcessors == null)
			{
				this.valueListProcessors = new List<AbstractProcessor>();
			}
			this.valueListProcessors.Add(p);
		}

		// Token: 0x0600121D RID: 4637 RVA: 0x00317DE8 File Offset: 0x00315FE8
		protected internal override void doFormat(TCustomSqlStatement stmtArg)
		{
			TInsertSqlStatement tinsertSqlStatement = stmtArg as TInsertSqlStatement;
			this.runProcessor<TInsertSqlStatement>(this.SpecialProcessors, tinsertSqlStatement);
			TObjectNameList columnList = tinsertSqlStatement.ColumnList;
			this.runProcessor<TObjectNameList>(this.itemListProcessors, columnList);
			TMultiTargetList values = tinsertSqlStatement.Values;
			this.processValues(values);
			if (tinsertSqlStatement.InsertIntoValues != null && tinsertSqlStatement.InsertIntoValues.Count > 0)
			{
				for (int i = 0; i < tinsertSqlStatement.InsertIntoValues.Count; i++)
				{
					TInsertIntoValue tinsertIntoValue = tinsertSqlStatement.InsertIntoValues[i];
					this.runProcessor<TObjectNameList>(this.itemListProcessors, tinsertIntoValue.ColumnList);
					this.processValues(tinsertIntoValue.TargetList);
				}
			}
			if (tinsertSqlStatement.SubQuery != null)
			{
				int num = SourceTokenOperator.curIndentLenVT(tinsertSqlStatement.startToken);
				EHiveInsertType hiveInsertType = tinsertSqlStatement.HiveInsertType;
				TSourceToken tsourceToken = SourceTokenSearcher.firstSelectedToken(tinsertSqlStatement.SubQuery.endToken.container, tinsertSqlStatement.SubQuery.startToken.posinlist, tinsertSqlStatement.SubQuery.endToken.posinlist, "select");
				if (tsourceToken != null)
				{
					SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, tsourceToken);
					SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createReturnSourceToken());
					SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(num + this.Option.indentLen));
				}
				if (tinsertSqlStatement.InsertIntoValues != null && tinsertSqlStatement.InsertIntoValues.Count > 0)
				{
					SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, tinsertSqlStatement.SubQuery.startToken);
					SourceTokenOperator.addBefore(this.Option, tinsertSqlStatement.SubQuery.startToken, SourceTokenOperator.createReturnSourceToken());
					SourceTokenOperator.addBefore(this.Option, tinsertSqlStatement.SubQuery.startToken, SourceTokenOperator.createWhitespaceSourceToken(num));
				}
				ExpressionProcessor.processParenthesesNodeInSubQuery(this.Option, tinsertSqlStatement.SubQuery, tinsertSqlStatement.SubQuery.startToken, tinsertSqlStatement.SubQuery.endToken);
			}
		}

		// Token: 0x0600121E RID: 4638 RVA: 0x00317FB0 File Offset: 0x003161B0
		private void processValues(TMultiTargetList multiTarget)
		{
			if (multiTarget != null)
			{
				for (int i = 0; i < multiTarget.size(); i++)
				{
					TMultiTarget multiTarget2 = multiTarget.getMultiTarget(i);
					if (multiTarget2.ColumnList != null && multiTarget2.ColumnList.size() > 0)
					{
						this.runProcessor<TResultColumnList>(this.valueListProcessors, multiTarget2.ColumnList);
						for (int j = 0; j < multiTarget2.ColumnList.size(); j++)
						{
							TResultColumn resultColumn = multiTarget2.ColumnList.getResultColumn(j);
							if (resultColumn.Expr != null)
							{
								this.runProcessor<TExpression>(this.ExpressionProcessors, resultColumn.Expr);
							}
						}
					}
					if (multiTarget2.SubQuery != null)
					{
						ExpressionProcessor.processParenthesesNodeInSubQuery(this.Option, multiTarget2.SubQuery, multiTarget2.SubQuery.startToken, multiTarget2.SubQuery.endToken);
					}
				}
			}
		}

		// Token: 0x040013DC RID: 5084
		private IList<AbstractProcessor> itemListProcessors;

		// Token: 0x040013DD RID: 5085
		private IList<AbstractProcessor> valueListProcessors;
	}
}
